package com.xin.util;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;

/**
 * created by l1 on 2017/11/17.
 */
public  class ReflectUtil {

    public static void emptyToNull(Object obj){
        Field[] fields = obj.getClass().getDeclaredFields();
        for(int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if(field.getType()== String.class){
                field.setAccessible(true);
                try {
                    String value = (String)field.get(obj);
                    if(StringUtils.isEmpty(value)){
                        field.set(obj,null);
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
//            if(fields[i].getName().equalsIgnoreCase(propertyName)) {
//                fields[i].set(obj, value);
//                break;
//            }
        }
    }
}
